package com.cloud.diffdemo

import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.widget.LinearLayout
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ItemDecoration
import com.cloud.diffdemo.adapter.UserAdapter
import com.cloud.diffdemo.model.UserBean

class MainActivity : AppCompatActivity() {
    private val recyclerView:RecyclerView by lazy { findViewById(R.id.recyclerView) }
    private lateinit var userAdapter:UserAdapter
    private var userList:MutableList<UserBean> = ArrayList()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        initData()
        initView()
    }

    private fun initData() {
        userList = ArrayList()
        for(i in 1..10){
            val userBean = UserBean(id = i, name = "Tome", age = 25)
            userList.add(userBean)
        }
    }

    private fun initView() {
        userAdapter = UserAdapter(this@MainActivity,userList)
        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.addItemDecoration(DividerItemDecoration(this, LinearLayout.VERTICAL))
        recyclerView.adapter = userAdapter

        Handler(Looper.getMainLooper()).postDelayed({
            userAdapter.data = getNewData()
        }, 2000)
    }

    private fun getNewData(): List<UserBean> {
        for (i in 1..10) {
            UserBean(id = i, name = "Jack", age = 20).let {
                userList.add(it)
            }
        }
        return userList
    }
}